<?php
namespace app\admin\controller;
use think\Request;
use think\Validate;
use think\Cache;
use app\common\lib\Htmlp;
use app\common\controller\AdminBase;

class ApiAdminAdmin extends AdminBase
{
    public function passwordEdit()
    {
        $oldPassword = Htmlp::htmlp(input('post.oldPassword'));
        $newPassword = Htmlp::htmlp(input('post.newPassword'));
        $rpassword = Htmlp::htmlp(input('post.rpassword'));
        if (!$oldPassword) {
            return jsonError('请输入原来密码');
        }
        if (!$newPassword){
             return jsonError('请输入新密码');
        }
        if (strlen($oldPassword) != 32) {
             return jsonError('密码长度不符合规则');
        }
        if (strlen($newPassword) != 32) {
             return jsonError('密码长度不符合规则');
        }
        if (strlen($rpassword) != 32) {
             return jsonError('密码长度不符合规则');
        }
        if ($oldPassword == $newPassword) {
            return jsonError('新密码不能与旧密码重复');
        }
        if($rpassword != $newPassword){
            return jsonError('两次输入的密码不一样');
        }
        $userPasswordInfo = db('BaseAdmin')->where('uid',$this->adminId)->find();
        $oldpassword = create_md5($oldPassword,$userPasswordInfo['salt']);
        if($oldpassword != $userPasswordInfo['password']) {
             return jsonError('旧密码错误，请重新输入');
        }
        $data['salt'] = create_salt(8);
        $data['password'] = create_md5($newPassword,$data['salt']);
        db('BaseAdmin')->where('uid',$this->adminId)->update($data);
        $adminToken = cookie('adminToken');
        db('BaseAdminToken')->where('token',$adminToken)->delete();
        model('app\common\model\Log')->itemAdd('修改密码，用户IP：' . Request::instance()->ip(),config('adminId'),'adminLogin');
        return  jsonSuccess('修改成功','','/login/');
    }
    
}